const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
const startDebugger = require("debug")("app:start");

const user = require("./routes/user");
const product = require("./routes/product");

mongoose
  .connect("mongodb://lly:123456@120.55.83.216:27017/wechat", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => startDebugger("mongoDB connected"))
  .catch((err) => startDebugger(err));

const app = express();

app.use(morgan("tiny"));
app.use(express.json());
// app.use(express.urlencoded({ extended: true }));
// app.use(express.static("public"));

app.use("/api/user", user);
app.use("/api/product", product);

app.get("/api/:id", (req, res) => {
  res.send(`Hello World, ${req.params.id}`);
});

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));
